package team.tunan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import team.tunan.entity.Game;
import team.tunan.entity.Score;
import team.tunan.entity.User;
import team.tunan.mapper.GameMapper;
import team.tunan.mapper.MatchesMapper;
import team.tunan.mapper.ScoreMapper;
import team.tunan.mapper.UserMapper;
import team.tunan.service.IScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements IScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MatchesMapper matchMapper;
    @Autowired
    private GameMapper gameMapper;

    @Override
    public Map<String, Object> doScore(Score score) {
        System.out.println(score);
        System.out.println(score);
        System.out.println(score);
        System.out.println(score);
        System.out.println(score.getSid());
        System.out.println(score.getSid());
        System.out.println(score.getSid());


        Integer jid1 = score.getJid();

        Map<String, Object> map = new HashMap<>();

        List<Score> score1 = scoreMapper.getScore(score);//搜索出本场次没有打分的记录
        if (score1.size() == 0){
            map.put("status", 1);
            map.put("flag", false);
            return map;
        }

        if (score.getJudgesScoring() == null) {
            map.put("status", 0);
            map.put("flag", false);
            return map;
        }


        Integer jid = scoreMapper.getJScore(score);
        if (jid == jid1 ) {
            System.out.println("不执行");
            map.put("flag", false);
            return map;
        }
        System.out.println("执行");


        // 执行打分
        int i = scoreMapper.addJudgesScore(score);
        scoreMapper.updateStatus(score.getMid(),score.getPlayerId());
//        int i = 1;

        if (i > 0) {
            map.put("flag", true);
            return map;
        }
        map.put("flag", false);
        return map;
    }

    @Override
    public List<Map<String, Object>> getRankingList() {
        List<Map<String, Object>> wins = scoreMapper.getWins();
        for (Map<String, Object> map : wins) {
            Integer player_id = (Integer)map.get("player_id");
            Game game = gameMapper.getGame((Integer) map.get("match_id"));
            User user = userMapper.getUserById(player_id);
            String username = user.getUsername();
            map.put("username",username);
            map.put("mname",game.getMname());
        }

        return wins;
    }

    @Override
    public List<Map<String, Object>> getStartedList(Integer gid) {
        List<Map<String, Object>> noWinsByGameAndRound = matchMapper.getNoWinsByGameAndRound(gid);
        for (Map<String, Object> m : noWinsByGameAndRound) {
            System.out.println(m);
            Integer aid = (Integer) m.get("player_a");
            Integer bid = (Integer) m.get("player_b");
            User a = userMapper.getUserById(aid);
            User b = userMapper.getUserById(bid);
            Game game = gameMapper.getGame((Integer) m.get("match_id"));

            m.put("a_name", a.getUsername());
            m.put("b_name", b.getUsername());
            m.put("mname",game.getMname());
        }

        return noWinsByGameAndRound;
    }

    @Override
    public List<Map<String, Object>> getStartedList2() {
        List<Map<String, Object>> noWinsByGameAndRound = matchMapper.getNoWinsByGameAndRound2();
        for (Map<String, Object> m : noWinsByGameAndRound) {
            System.out.println(m);
            Integer aid = (Integer) m.get("player_a");
            Integer bid = (Integer) m.get("player_b");
            User a = userMapper.getUserById(aid);
            User b = userMapper.getUserById(bid);
            Game game = gameMapper.getGame((Integer) m.get("match_id"));

            m.put("a_name", a.getUsername());
            m.put("b_name", b.getUsername());
            m.put("mname",game.getMname());
        }

        return noWinsByGameAndRound;
    }

    @Override
    public List<Map<String, Object>> getRankingList2(Integer gid,Integer roundNum) {
        List<Map<String, Object>> wins = scoreMapper.getWinsByGameAndRound(gid, roundNum);
        for (Map<String, Object> map : wins) {
            Integer player_id = (Integer)map.get("player_id");
            Game game = gameMapper.getGame(gid);
            User user = userMapper.getUserById(player_id);
            String username = user.getUsername();
            map.put("username",username);
            map.put("mname",game.getMname());

        }

        return wins;
    }
}
